package rcs34.android.photopublishare;

import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class ActivitySetTarget extends Activity {
	private final String LOGTAG = getClass().getName();
	private Intent data = new Intent();
	private EditText name;
	private EditText url;
	private EditText login;
	private EditText password;
	private EditText port;
	private String rowid;
	private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_target);
		name = (EditText) findViewById(R.id.targetName);
		url = (EditText) findViewById(R.id.targetUrl);
		login = (EditText) findViewById(R.id.targetLogin);
		password = (EditText) findViewById(R.id.targetPassword);
		port = (EditText) findViewById(R.id.targetPort);
		Intent intent = getIntent();
		if (intent.hasExtra(DBHelper.COL_ROWID)) {
			rowid = intent.getExtras().getString(DBHelper.COL_ROWID);
		}
		fillText(intent, DBHelper.COL_NAME, name);
		fillText(intent, DBHelper.COL_URL, url);
		fillText(intent, DBHelper.COL_LOGIN, login);
		fillText(intent, DBHelper.COL_PASSWORD, password);
		fillText(intent, DBHelper.COL_PORT, port);
	}

	private void fillText(Intent _intent, String _key, EditText _editText) {
		if (_intent.hasExtra(_key)) {
			_editText.setText(_intent.getExtras().getString(_key));
		} else {
			_editText.setText("");
		}
	}

	public void handleSave(View _view) {
		if (name.getText().toString().equals("")) {
			Toast.makeText(this, R.string.err_empty_name, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (url.getText().toString().equals("")) {
			Toast.makeText(this, R.string.err_empty_url, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		try {
			URL testUrl = new URL(url.getText().toString());
		} catch (MalformedURLException e) {
			Toast.makeText(this, R.string.err_invalid_url, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		String portString = port.getText().toString();
		if (!portString.equals("")) {
			try {
				int portNumber = Integer.parseInt(portString);
				if (portNumber < 1 || portNumber > 65535) {
					Toast.makeText(this, R.string.err_invalid_port,
							Toast.LENGTH_SHORT).show();
					return;
				}
			} catch (NumberFormatException _ex) {
				Toast.makeText(this, R.string.err_invalid_port,
						Toast.LENGTH_SHORT).show();
				return;
			}

		}
		progressDialog = ProgressDialog.show(this, "Saving", "");
		Saver saver = new Saver();
		saver.execute(0);
	}

	public void handleCancel(View _view) {
		setResult(Activity.RESULT_CANCELED, data);
		finish();
	}

	private class Saver extends AsyncTask<Integer, Integer, Integer> {
		@Override
		protected Integer doInBackground(Integer... _dummy) {
			ContentValues values = new ContentValues();
			if (rowid != null) {
				values.put(DBHelper.COL_ROWID, rowid);
			}
			values.put(DBHelper.COL_NAME, name.getText().toString().trim());
			values.put(DBHelper.COL_URL, url.getText().toString().trim());
			values.put(DBHelper.COL_LOGIN, login.getText().toString().trim());
			values.put(DBHelper.COL_PASSWORD, password.getText().toString()
					.trim());
			values.put(DBHelper.COL_PORT, port.getText().toString().trim());
			values.put(DBHelper.COL_SELECTED, "1");
			DBHelper dbHelper = new DBHelper();
			dbHelper.updateRecord(values);
			return 0;
		}

		@Override
		protected void onPostExecute(Integer _dummy) {
			progressDialog.dismiss();
			setResult(Activity.RESULT_OK, data);
			finish();
		}
	}

}
